package register.servlet;

import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import register.check.user.impl.CheckUserImpl;
import register.pojo.User;
import register.pojo.dao.UserDao;
import register.pojo.dao.impl.UserDaoImpl;

/**
 * Servlet implementation class Register
 */
@WebServlet("/register")
public class Register extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Register() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		UserDao  udi=new UserDaoImpl();
//		response.setContentType("text/;charset=UTF-8");
		request.setCharacterEncoding("UTF-8");
		String name=new String(request.getParameter("uname").getBytes(),"UTF-8");
		String sal=request.getParameter("sal");
		String sex=request.getParameter("sex");
		String year=request.getParameter("year");
		String month=request.getParameter("month");
		String day=request.getParameter("day");
		byte[] uhead=new byte[256*1024];
		InputStream is=request.getInputStream();
		int m=is.read(uhead);
		System.out.println(name);
		if(CheckUserImpl.isSal(sal)==true)
		{
			User user=new User();
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-mm-dd");
			try {
				Date date=sdf.parse(year+"-"+month+"-"+day);
				user.setName(name);
				user.setSal(Double.parseDouble(sal));
				user.setSex(sex);
				user.setBirth(date);
				user.setHead(uhead);
				udi.add(user);
				response.sendRedirect("success");
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		else {
			response.sendRedirect("failed");
		}
		
//		response.sendRedirect("success");
		
	}

}
